package com.hamster.crm.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hamster.crm.mapper.LeverageConfigMapper;
import com.hamster.crm.mapper.LeverageMapper;
import com.hamster.crm.pojo.Leverage;
import com.hamster.crm.pojo.LeverageConfig;
import com.hamster.crm.service.LeverageService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 后台杠杆计划的service
 * @author Tarry
 * @create 2019/12/19 10:52
 */
@Service
public class LeverageServiceImpl implements LeverageService {
    @Autowired
    private LeverageMapper leverageMapper;

    @Autowired
    private LeverageConfigMapper leverageConfigMapper;
    /**
     * 插入一条新的杠杆计划
     * @param leverage
     */
    @Override
    public void insertLeverage(Leverage leverage) {
        leverage.setInputUser("功能待完善");
        leverage.setInputTime(new Date());
        leverage.setDel("0"); //0代表正常，1代表删除
        leverageMapper.insert(leverage);
    }

    /**
     * 删除杠杆计划
     * @param ids
     */
    @Override
    @Transactional
    public void deleteLeverage(List<String> ids) {
        for (String id : ids) {
            leverageMapper.deleteTag(id);
        }
    }




    /**
     * 修改杠杆计划
     * @param leverage
     */
    @Override
    public void updateLeverage(Leverage leverage) {
        leverage.setInputTime(new Date());
        leverage.setInputUser("功能待完善");

        leverageMapper.updateById(leverage);
    }

    /**
     * 查询所有杠杆计划
     * @return
     */
    @Override
    public List<Leverage> listLeverages() {
        return leverageMapper.selectAll();
    }

    /**
     * 更新杠杆参与人提成比例的设置
     * @param leverageConfig
     */
    @Override
    public void updateLeverageConfig(LeverageConfig leverageConfig) {
        leverageConfig.setId("1");
        leverageConfig.setInputTime(new Date());
        leverageConfig.setInputUser("功能的待完善");
        //记录不存在则插入，存在则修改
        if (leverageConfigMapper.selectById("1") == null){
            leverageConfigMapper.insert(leverageConfig);
        }else{
            leverageConfigMapper.updateById(leverageConfig);
        }
    }

    /**
     * 获取杠杆参与人提成比例
     * @return LeverageConfig 提成比例
     */
    @Override
    public LeverageConfig getLeverageConfig() {
        return leverageConfigMapper.selectById(1);
    }

    /**
     * 根据 id查询杠杆计划
     * @param id
     * @return 杠杆计划
     */
    @Override
    public Leverage getLeverageById(String id) {
        QueryWrapper<Leverage> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("del","0").eq("id",id);
        Leverage leverage = leverageMapper.selectOne(wrapper2);
        return leverage;
    }
}
